<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-left">
        <div class="logo-container">
          <i class="el-icon-reading logo-icon"></i>
          <h2 class="welcome-text">欢迎使用<br>自习室预约系统</h2>
          <p class="slogan">安静学习 · 高效预约</p>
        </div>
      </div>
      <div class="login-right">
        <el-form 
          ref="loginForm"
          class="login-form" 
          :model="loginForm" 
          :rules="loginRules"
        >
          <h3 class="title">用户登录</h3>
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              prefix-icon="el-icon-user"
              placeholder="请输入用户名"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              prefix-icon="el-icon-lock"
              type="password"
              placeholder="请输入密码"
              @keyup.enter.native="handleLogin"
            />
          </el-form-item>
          <el-form-item>
            <el-button 
              :loading="loading"
              type="primary" 
              class="login-button"
              @click="handleLogin"
            >
              {{ loading ? '登录中...' : '登录' }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import request from '@/utils/request'

export default {
  name: 'LoginPage',
  data() {
    return {
      loading: false,
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(async valid => {
        if (valid) {
          this.loading = true
          try {
            const res = await request({
              url: '/api/api/auth/login',
              method: 'post',
              data: this.loginForm
            })
            // 存储token
            localStorage.setItem('token', res.data.token)
            // 存储权限信息
            localStorage.setItem('permissions', JSON.stringify(res.data.permissions))
            //存储userId
            localStorage.setItem('userId', res.data.userId)
            this.$message.success('登录成功')
            // 修改跳转路径为后台首页
            this.$router.push('/admin/system/user')
          } catch (error) {
            console.error('登录失败:', error)
            if (error.response && error.response.status === 401) {
              this.$message.error('账号或密码错误')
            } else {
              this.$message.error('登录失败，请稍后重试')
            }
          } finally {
            this.loading = false
          }
        }
      })
    }
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f0f2f5;
}

.login-box {
  width: 900px;
  height: 500px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  display: flex;
  overflow: hidden;
}

.login-left {
  position: relative;
  width: 50%;
  background: linear-gradient(135deg, #1890ff 0%, #36a5ff 100%);
  display: flex;
  align-items: center;
  justify-content: center;
}

.logo-container {
  text-align: center;
  z-index: 1;
}

.logo-icon {
  font-size: 80px;
  color: white;
  margin-bottom: 20px;
}

.welcome-text {
  color: white;
  font-size: 28px;
  text-align: center;
  line-height: 1.5;
  margin-bottom: 20px;
}

.slogan {
  color: rgba(255, 255, 255, 0.9);
  font-size: 16px;
  margin-top: 20px;
}

.login-right {
  width: 50%;
  padding: 40px;
  display: flex;
  align-items: center;
}

.login-form {
  width: 100%;
}

.title {
  text-align: center;
  margin-bottom: 40px;
  font-size: 24px;
  color: #333;
}

.login-button {
  width: 100%;
  height: 40px;
  font-size: 16px;
}

.el-input {
  height: 40px;
}

.el-input__inner {
  height: 40px;
  line-height: 40px;
}
</style> 